home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d14 / atre12.arc / MULTIPLX.ARC / MULT.C < prev    next >
C/C++ Source or Header  |  1991-07-27  |  3KB  |  123 lines

  1. /* A three control bit multiplexor test (11 bits total) */
  2.  
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include "atree.h"
  6.  
  7. #define TRAINSETSIZE 500
  8. #define TREESIZE 1024
  9. #define WIDTH 11
  10. #define TESTSIZE 500
  11.  
  12. #define Printf(str,fmt1,fmt2) \
  13.             { \
  14.             char Buff[80]; \
  15.             sprintf(Buff, str, fmt1, fmt2); \
  16.             MessageBox(hwnd, Buff, "Multiplexor", MB_OK); \
  17.             }
  18.  
  19. char multiplexor(v)
  20.  
  21. char *v;
  22.  
  23. {
  24.     return(v[(int)(((v[0] << 2) | (v[1] << 1) | v[2]) + 3)]);
  25. }
  26.  
  27. BOOL NEAR PASCAL main(HWND hwnd)
  28. {
  29.     int i;
  30.     int j;
  31.     LPBIT_VEC training_set;
  32.     LPBIT_VEC icres;
  33.     LPBIT_VEC test;
  34.     char vec[WIDTH];
  35.     char ui[1];
  36.     int correct = 0;
  37.     LPATREE tree;
  38.     HCURSOR hCursor;
  39.  
  40.     /* Initialise */
  41.  
  42.     training_set = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
  43.     MEMCHECK(training_set);
  44.  
  45.     icres = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
  46.     MEMCHECK(icres);
  47.  
  48.     atree_init();
  49.  
  50.     /* Create the test data */
  51.  
  52.     Printf("Creating training data\n",0,0);   /*Printf macro requires 2 args*/
  53.  
  54.     for (i = 0; i < TRAINSETSIZE; i++)
  55.     {
  56.         /* allow multitasking during long loop! */
  57.         Windows_Interrupt(2000);
  58.  
  59.         for (j = 0; j < WIDTH; j++)
  60.         {
  61.             vec[j] = RANDOM(2);
  62.         }
  63.         training_set[i] = *(bv_pack(vec,WIDTH));
  64.         ui[0] = multiplexor(vec);
  65.         icres[i] = *(bv_pack(ui,1));
  66.     }
  67.  
  68.     /* Create a tree and train it */
  69.  
  70.     Printf("Training tree\n",0,0);
  71.  
  72.     tree = atree_create(WIDTH,TREESIZE);
  73.     (void) atree_train(tree,training_set,icres,0,TRAINSETSIZE,
  74.                        TRAINSETSIZE-1,100,1);
  75.  
  76.     /* Test the trained tree */
  77.  
  78.     Printf("Testing the tree\n",0,0);
  79.  
  80.     for (i = 0; i < TESTSIZE; i++)
  81.     {
  82.         /* allow multitasking during long loop! */
  83.         Windows_Interrupt(2000);
  84.  
  85.         for (j = 0; j < WIDTH; j++)
  86.         {
  87.             vec[j] = RANDOM(2);
  88.         }
  89.         test = bv_pack(vec,WIDTH);
  90.         if (atree_eval(tree,test) == multiplexor(vec))
  91.         {
  92.             correct++;
  93.         }
  94.         bv_free(test);
  95.     }
  96.  
  97.     Printf("%d correct out of %d in final test\n",correct,TESTSIZE);
  98.  
  99.     /* Discard training set */
  100.  
  101.     Printf("Please wait, discarding training set",0,0);
  102.  
  103.     hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  104.     ShowCursor(TRUE);
  105.  
  106.     for (i = 0; i < TRAINSETSIZE; i++)
  107.         {
  108.         Free(training_set[i].bv);
  109.         Free(icres[i].bv);
  110.         }
  111.  
  112.     Free(training_set);
  113.     Free(icres);
  114.  
  115.     ShowCursor(FALSE);
  116.     SetCursor(hCursor);
  117.  
  118.     /* Discard tree */
  119.     atree_free(tree);
  120.  
  121.     return TRUE;
  122. }
  123.